define([
    'jquery',
    'underscore',
    'backbone',
    'module/brand/models/BrandModel',
    'module/brand/models/BrandCollection',
    'module/brand/views/List',
    'text!module/brand/templates/main.html',
    'text!module/brand/templates/add.html',
    'text!module/brand/templates/edit.html',
    'text!module/brand/templates/drop.html',
], function($, _, Backbone, BrandModel, BrandCollection, List, main, add, edit, drop) {

    var BrandController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.render();
            this.list_view();
        },
        events: {
            "click #list_view": "list_view",
            "click #add_view": "add_view",
            "click #edit_view": "edit_view",
            "click #drop_view": "drop_view",
            "click #add_action": "add_action",
            "click #edit_action": "edit_action",
            "click #drop_action": "drop_action",
            "click #search": "search",
            "click #pag": "pag_action"
        },
        render: function() {
            this.$el.html(main);
        },
        list_view: function() {
            $.ajax({
                url: 'server/api/brand/find_all/1',
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new BrandCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        },
        add_view: function() {
            $("#main-list").html(add);
        },
        edit_view: function(event) {
            $("#main-list").html(edit);
            var id = event.target.getAttribute('index');
            var brand = new BrandModel({id: id});
            brand.fetch({
                success: function(data) {
                    data = data.toJSON()
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                }
            });
        },
        drop_view: function(event) {
            $("#main-list").html(drop);
            var id = event.target.getAttribute('index');
            var brand = new BrandModel({id: id});
            brand.fetch({
                success: function(data) {
                    data = data.toJSON()
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                }
            });
        },
        add_action: function() {
            var self = this;
            var brand = new BrandModel();
            var brandDetails = {
                name: $("#name").val()
            };
            brand.save(brandDetails, {
                success: function(data) {
                    self.list_view();
                }
            });
        },
        edit_action: function() {
            var self = this;
            var brand = new BrandModel();
            var brandDetails = {
                id: $("#id").val(),
                name: $("#name").val()
            };
            brand.save(brandDetails, {
                success: function(data) {
                    self.list_view();
                }
            });
        },
        drop_action: function() {
            var self = this;
            var brand = new BrandModel({
                id: $("#id").val()
            });
            brand.destroy({
                success: function(data) {
                    self.list_view();
                }
            });
        },
        search: function() {
            var search = $('#txt_search').val();
            $.ajax({
                url: 'server/api/brand/search/' + search,
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new BrandCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        },
        pag_action: function(event) {
            var id = event.target.getAttribute('index');
            $.ajax({
                url: 'server/api/brand/find_all/' + id,
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new BrandCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        }
    });
    return BrandController;
});
